package com.quartz.monitor4job.listener;

import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.Trigger.CompletedExecutionInstruction;
import org.quartz.TriggerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.quartz.monitor4job.model.QuartzLog;
import com.quartz.monitor4job.service.QuartzLogService;
import com.quartz.monitor4job.util.ApplicationUtil;
import com.quartz.monitor4job.util.QuartLogClearThread;

/**
 * 触发器监听器
 * @author LIPENG
 *
 * @date 2017年5月5日 上午11:46:30
 *
 * @version V1.0
 */
public class MonitorTriggerListener implements TriggerListener {
    
	private static final Logger logger = LoggerFactory.getLogger(MonitorTriggerListener.class);
    private ThreadLocal<QuartzLog> localLog=new ThreadLocal<QuartzLog>();
    
    /**    
     * 创建一个新的实例 MonitorTriggerListener.      
     */
    public MonitorTriggerListener() {
       if(!QuartLogClearThread.isInit()){
    	   new Thread(QuartLogClearThread.getInstance()).start();
       } 
    }
    /**  
     * @see org.quartz.TriggerListener#getName()    
     */
    @Override
    public String getName() {
        return "MonitorTriggerListener";
    }
    
    @Override
	public void triggerComplete(Trigger trigger, JobExecutionContext context,
			CompletedExecutionInstruction triggerInstructionCode) {
    	 try {
             QuartzLog quartzLog=localLog.get();
             if(quartzLog==null)return ;
             quartzLog.setExeTime(context.getJobRunTime());
             QuartzLogService quartzLogService=ApplicationUtil.getBean("quartzLogService");
             quartzLogService.update(quartzLog);
         } catch (Exception e) {
             logger.error("记录job结束时间异常",e);
         }catch (Throwable e) {
             logger.error("记录job结束时间出错",e);
         }
		
	}
    
    /**  
     * @see org.quartz.TriggerListener#triggerFired(org.quartz.Trigger, org.quartz.JobExecutionContext)    
     */
    @Override
    public void triggerFired(Trigger arg0, JobExecutionContext jec) {
        try {
            JobDetail jobDetail = jec.getJobDetail();
            //System.out.println(localLog.get());
            QuartzLogService quartzLogService=ApplicationUtil.getBean("quartzLogService");
            QuartzLog quartzLog=quartzLogService.insert(jobDetail.getKey().getName());
            localLog.set(quartzLog);
           // System.out.println(quartzLog);
        } catch (Exception e) {
            logger.error("记录job开始时间异常",e);
        }catch (Throwable e) {
            logger.error("记录job开始时间出错",e);
        }
    }
    
    /**  
     * @see org.quartz.TriggerListener#triggerMisfired(org.quartz.Trigger)    
     */
    @Override
    public void triggerMisfired(Trigger arg0) {
    }
    
    /**  
     * @see org.quartz.TriggerListener#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext)    
     */
    @Override
    public boolean vetoJobExecution(Trigger arg0, JobExecutionContext arg1) {
        return false;
    }
}